﻿Public Class ADDAGENT
    Dim MAGENT As MASAGENT = New MASAGENT
    Dim connect As CONDB = CONDB.NewConnection()
    Private Sub AddCus_Click(sender As Object, e As EventArgs) Handles AddAg.Click
        INSERTAGENT()
    End Sub
    Public Sub INSERTAGENT()
        If AGENTNAME.Text.Length > 0 Then
            MAGENT.AGENTNAME_ = AGENTNAME.Text
            MAGENT.AGENTLASTNAME_ = AGENTLASTNAME.Text
            MAGENT.AGENTADD_ = AGENTADD.Text
            'MAGENT.AGENTEMAIL_ = AGENTEMAIL.Text
            MAGENT.AGENTTEL_ = AGENTTEL.Text
            If Convert.ToString(MASCHANGWAT.Tag).Trim <> "" Then
                MAGENT.AGENTCHANGWAT_ = MASCHANGWAT.Tag
            Else
                MAGENT.AGENTCHANGWAT_ = "0"
            End If
            If Convert.ToString(MASAMPUR.Tag).Trim <> "" Then
                MAGENT.AGENTAMPUR_ = MASAMPUR.Tag
            Else
                MAGENT.AGENTAMPUR_ = "0"
            End If
            MAGENT.INSERTMASAGENT()
            SelectDatagridMasAgent()
        Else
            MsgBox("กรุณากรอกข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
        End If

    End Sub
    Private Sub SelectDatagridMasAgent()
        Dim objSelectDataMASAGENT As New MASAGENT
        Dim MASAGENT As DataTable
        MASAGENT = connect.GetTable(objSelectDataMASAGENT.SELECTMASAGENT_)
        AGENTGrid.PrimaryGrid.DataSource = MASAGENT
    End Sub
    Public Sub LOADFILTERCLASS()
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(idmaschangwat AS CHAR(4)) AS idmaschangwat, changwat FROM maschangwat WHERE STATUS = '" & 1 & "'"
        s = New FILTERCLASS(MASCHANGWAT, sql, "รหัสจังหวัด,จังหวัด", "40,110", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True

    End Sub
    Public Sub LOADAMPUR(CODECHANGWAT As String)
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(idmasampur AS CHAR(4)) AS idmasampur, ampur FROM masampur WHERE idmaschangwat = '" & CODECHANGWAT & "'"
        s = New FILTERCLASS(MASAMPUR, sql, "รหัสอำเภอ,อำเภอ", "40,110", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True

    End Sub
    Private Sub ADDAGENT_Load(sender As Object, e As EventArgs) Handles Me.Load
        LOADFILTERCLASS()
        SelectDatagridMasAgent()
    End Sub

    Private Sub MASCHANGWAT_TextChanged(sender As Object, e As EventArgs) Handles MASCHANGWAT.TextChanged
        If Convert.ToString(MASCHANGWAT.Tag).Trim <> "" Then
            LOADAMPUR(MASCHANGWAT.Tag)
        End If

    End Sub

    Private Sub AGENTGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles AGENTGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If grid.Cells("รหัส").Value() IsNot DBNull.Value Then
            AGENTIDTxt.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัส").Value()
        Else
            AGENTIDTxt.Text = ""
        End If
        If grid.Cells("ชื่อตัวแทน").Value() IsNot DBNull.Value Then
            AGENTNAME.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อตัวแทน").Value()
        Else
            AGENTNAME.Text = ""
        End If
        If grid.Cells("นามสกุล").Value() IsNot DBNull.Value Then
            AGENTLASTNAME.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("นามสกุล").Value()
        Else
            AGENTLASTNAME.Text = ""
        End If
        If grid.Cells("ที่อยู่").Value() IsNot DBNull.Value Then
            AGENTADD.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ที่อยู่").Value()
        Else
            AGENTADD.Text = ""
        End If
        If grid.Cells("เบอร์โทรศัพท์").Value() IsNot DBNull.Value Then
            AGENTTEL.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เบอร์โทรศัพท์").Value()
        Else
            AGENTTEL.Text = ""
        End If
        If grid.Cells("agentchangwat").Value() IsNot DBNull.Value Then
            MASCHANGWAT.Tag = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("agentchangwat").Value()
        Else
            MASCHANGWAT.Tag = ""
        End If
        If grid.Cells("จังหวัด").Value() IsNot DBNull.Value Then
            MASCHANGWAT.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จังหวัด").Value()
        Else
            MASCHANGWAT.Text = ""
        End If
        If grid.Cells("agentampur").Value() IsNot DBNull.Value Then
            MASAMPUR.Tag = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("agentampur").Value()
        Else
            MASAMPUR.Tag = ""
        End If
        If grid.Cells("อำเภอ").Value() IsNot DBNull.Value Then
            MASAMPUR.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("อำเภอ").Value()
        Else
            MASAMPUR.Text = ""
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE cpm.masagent SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`STATUS` = 1 "
                Else
                    Sql += "`STATUS` = 0 "
                End If
                Sql += "WHERE `agentid` = '" & AGENTIDTxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        AddAg.Enabled = False
    End Sub

    Private Sub EditAg_Click(sender As Object, e As EventArgs) Handles EditAg.Click
        If AGENTIDTxt.Text.ToString.Length > 0 Then
            Dim editStat As Integer
            editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
            If editStat = 1 Then
                Dim objeditData As New MASAGENT
                objeditData.AGENTID_ = AGENTIDTxt.Text
                objeditData.AGENTNAME_ = AGENTNAME.Text
                objeditData.AGENTLASTNAME_ = AGENTLASTNAME.Text
                objeditData.AGENTADD_ = AGENTADD.Text
                objeditData.AGENTTELL_ = AGENTTEL.Text
                If Convert.ToString(MASAMPUR.Tag).Trim <> 0 Then
                    objeditData.AGENTAMPUR_ = Convert.ToString(MASAMPUR.Tag).Trim
                End If
                If Convert.ToString(MASCHANGWAT.Tag).Trim <> 0 Then
                    objeditData.AGENTCHANGWAT_ = Convert.ToString(MASCHANGWAT.Tag).Trim
                End If
                objeditData.UPDATEMASAGENT()

                SelectDatagridMasAgent()

                'cleardata()
            End If
        Else
            MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
        End If
        AddAg.Enabled = True
    End Sub
    Private Sub cleardata()
        AGENTNAME.Text = ""
        AGENTIDTxt.Text = ""
        AGENTLASTNAME.Text = ""
        AGENTTEL.Text = ""
        AGENTADD.Text = ""
        MASCHANGWAT.Text = ""
        MASAMPUR.Text = ""
        SelectDatagridMasAgent()
    End Sub

    Private Sub ButtonX1_Click(sender As Object, e As EventArgs) Handles ButtonX1.Click
        cleardata()
    End Sub
End Class